package com.powernode.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.constant.BusinessEnum;
import com.powernode.domain.Prod;
import com.powernode.domain.Sku;
import com.powernode.model.ChangeStock;
import com.powernode.model.Result;
import com.powernode.service.ProdService;
import com.powernode.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @create: 2024/7/18
 * @author: gyhe10
 * @description: 商品管理控制层
 **/

@Api(tags = "商品接口管理")
@RestController
@RequestMapping("prod/prod")
public class ProdController {

    @Resource
    private ProdService prodService;
    @Resource
    private SkuService skuService;

    /**
     * 多条件分页查询商品列表
     * @param current    页码
     * @param size       每页条数
     * @param prodName   商品名称
     * @param status     商品状态
     * @return
     */
    @ApiOperation("多条件分页查询商品列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(@RequestParam Long current,
                                           @RequestParam Long size,
                                           @RequestParam(required = false) String prodName,
                                           @RequestParam(required = false) Integer status) {
        Page<Prod> page = new Page<>(current, size);

        page = prodService.page(page, new LambdaQueryWrapper<Prod>()
                .eq(ObjectUtil.isNotNull(status), Prod::getStatus, status)
                .like(StringUtils.hasText(prodName), Prod::getProdName, prodName)
                .orderByDesc(Prod::getCreateTime));

        return Result.success(page);
    }

    /**
     * 新增商品
     * @param prod 商品对象
     * @return
     */
    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> saveProd(@RequestBody Prod prod) {
        Boolean saved = prodService.saveProd(prod);
        return Result.handle(saved);
    }

    /**
     * 根据商品ID查询商品详情
     * @param prodId 商品ID
     * @return
     */
    @ApiOperation("根据商品ID查询商品详情")
    @GetMapping("info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> loadProdInfo(@PathVariable Long prodId) {
        Prod prod = prodService.queryProdById(prodId);
        return Result.success(prod);
    }

    /**
     * 修改商品
     * @param prod 商品对象
     * @return
     */
    @ApiOperation("修改商品")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> modifyProd(@RequestBody Prod prod) {
        Boolean modified = prodService.modifyProd(prod);
        return Result.handle(modified);
    }

    /**
     * 批量/单个删除商品
     * @param prodIds 商品ID列表
     * @return
     */
    @ApiOperation("单个/批量删除商品")
    @DeleteMapping("{prodIds}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public Result<String> removeProds(@PathVariable List<Long> prodIds) {
        Boolean removed = prodService.removeProdByIds(prodIds);
        return Result.handle(removed);
    }

    ///////////////// feign 接口 ////////////////

    /**
     * feign接口：根据商品ID列表查询商品列表
     * @param prodIds
     * @return
     */
    @GetMapping("getProdListByIds")
    public Result<List<Prod>> getProdListByIds(@RequestParam List<Long> prodIds) {
        // 模拟异常
        // int i = 1 / 0;
        List<Prod> prodList = prodService.listByIds(prodIds);
        // 判断集合是否为空
        if (CollectionUtil.isNotEmpty(prodList) && prodList.size() > 0) {
            return Result.success(prodList);
        } else {
            return Result.fail(BusinessEnum.OPERATION_FAIL);
        }
    }

    @GetMapping("getProdListByCategoryIds")
    public Result<List<Prod>> getProdListByCategoryIds(@RequestParam List<Long> categoryIds) {
        List<Prod> list = prodService.list(new LambdaQueryWrapper<Prod>()
                .in(Prod::getCategoryId, categoryIds));
        return Result.success(list);
    }

    /**
     * 根据skuID列表查询sku列表
     * @param skuIdList
     * @return
     */
    @GetMapping("getSkuListByIds")
    public Result<List<Sku>> getSkuListByIds(@RequestParam List<Long> skuIdList) {
        List<Sku> skus = skuService.listByIds(skuIdList);
        return Result.success(skus);
    }

    /**
     * 修改商品prod和sku库存
     * @param changeStock
     * @return
     */
    @PostMapping("changeProdAndSkuStock")
    public Result<Boolean> changeProdAndSkuStock(@RequestBody ChangeStock changeStock) {
        Boolean changed = prodService.changeProdAndSkuStock(changeStock);
        return Result.success(changed);
    }

    ///////////////// 微信小程序接口 ////////////////

    /**
     * 小程序根据商品id查询商品详情
     * @param prodId 商品ID
     * @return
     */
    @ApiOperation("小程序根据商品id查询商品详情")
    @GetMapping("prod/prodInfo")
    public Result<Prod> loadWxProdInfo(@RequestParam Long prodId) {
        Prod prod = prodService.queryWxProdInfoById(prodId);
        return Result.success(prod);
    }


}
